module cl {

	export class ForceCapture {
		public constructor(data : CampaignData | CampaignDataEx) {
			this.forceMap = Util.createArray(Map.tileEngine.tileCount, -1);
			this.minpayMap = Util.createArray(Map.tileEngine.tileCount, 50000);
			// 部队到达
			for (let troopid = 0; troopid < data.troop.length; troopid++){
				let troop = data.troop[troopid];
				if (data.troop_type[troop.troop_type].plane != 0 || troop.strength <= 0){
					continue;
				}

				let troopPayMap = Map.routeEngine.payMap(data.zone.pass_index, Map.tileEngine.tile2tid(troop.tile), 50000).payMap;
				for (let idx = 0; idx < this.minpayMap.length; idx++){
					let troopPay = troopPayMap[idx];
					troopPay = troopPay - (troopPay < 1 ? 0 : 0.0001 * troop.move_remain);
					if (troopPay < this.minpayMap[idx]){
						this.minpayMap[idx] = troopPay;
						this.forceMap[idx] = troop.force;
					}
				}
			}
			// 势力点到达
			for (let forceid = 0; forceid < data.force.length; forceid++){
				let forcepointPayMap = Map.routeEngine.payMap(data.zone.pass_index, Map.tileEngine.tile2tid(data.force[forceid].tile), 50000).payMap;
				for (let idx = 0; idx < this.minpayMap.length; idx++){
					if (forcepointPayMap[idx] < this.minpayMap[idx]){
						this.minpayMap[idx] = forcepointPayMap[idx];
						this.forceMap[idx] = forceid;
					}
				}
			}
		}

		public forceMap : number[];
		public minpayMap : number[];
	}

	export class ForceMapUtil {
		public static refreshTile(forceMap : ForceMap, zone : Zone, tid : number) : void {
			forceMap.see_map[tid] = true;
			forceMap.pass_index[tid] = zone.pass_index[tid];
			forceMap.defend_index[tid] = zone.defend_index[tid];
		}

		public static refreshTileAndNeighbor(forceMap : ForceMap, zone : Zone, tid : number) : void {
			ForceMapUtil.refreshTile(forceMap, zone, tid);
			for (let tgttid of Map.tileEngine.neighborCache[tid]){
				ForceMapUtil.refreshTile(forceMap, zone, tgttid);
			}
		}

		public static refreshByCapture(forceid : number, forceMap : ForceMap, zone : Zone, forceCapture : ForceCapture) : void {
			forceMap.see_map = Util.createArray(Map.tileEngine.tileCount, false);

			for (let tid = 0; tid < Map.tileEngine.tileCount; tid++){
				if (forceCapture.forceMap[tid] == forceid){
					this.refreshTileAndNeighbor(forceMap, zone, tid);
				}
			}
		}

		public static refreshAll(data : CampaignData | CampaignDataEx) : void {
			let capture = new ForceCapture(data);
			for (let forceid = 0; forceid < data.force.length; forceid++) {
				ForceMapUtil.refreshByCapture(forceid, data.force[forceid].force_map, data.zone, capture);
			}
		}

		public static initForceMap(data : CampaignData | CampaignDataEx) : void {
			let capture = new ForceCapture(data);
			for (let forceid = 0; forceid < data.force.length; forceid++) {
				let forceMap = data.force[forceid].force_map;
				forceMap.defend_index = data.zone.defend_index.concat();
				forceMap.pass_index = data.zone.pass_index.concat();
				ForceMapUtil.refreshByCapture(forceid, forceMap, data.zone, capture);
			}
		}
	}




	/*
	// 标识每个势力已收集的区域情报
	// 1.每个tick扫描战争迷雾，并获取其情报
	// 2.只要部队经过就扫描，每个tick可能搜集多次）
	export class ForceInfo {
		// 1.获得forceCapture及其邻域为可视区域
		// 2.获得所有军队是否可见的信息
		// 3.function 通过forceCapture刷新ForceInfo
		// 4.function 通过troop移动后所在位置，动态刷新ForceInfo
		// 5.function 获得当前的通过地图，敌军部队所在地区视为不可通过（障碍指数50000）
		public constructor(public id : number, forceCapture : ForceCapture) {
			this.pass_index = Campaign.zone.pass_index.concat();
			this.defend_index = Campaign.zone.defend_index.concat();
			this.refreshByCapture(forceCapture);
		}

		public seeMap : boolean[];
		public pass_index : number[];
		public defend_index : number[];

		public refreshTile(tid) : void {
			this.seeMap[tid] = true;
			this.pass_index[tid] = Campaign.zone.pass_index[tid];
			this.defend_index[tid] = Campaign.zone.defend_index[tid];
		}

		public refreshByCapture(forceCapture : ForceCapture) : void {
			this.seeMap = Util.createArray(Map.tileEngine.tileCount, false);

			for (let tid = 0; tid < Map.tileEngine.tileCount; tid++){
				if (forceCapture.forceMap[tid] == this.id){
					this.refreshTileAndNeighbor(tid);
				}
			}
		}

		public refreshTileAndNeighbor(tid) : void {
			if (this.seeMap[tid] == false){
				this.refreshTile(tid);
			}
			for (let tgttid of Map.tileEngine.neighborCache[tid]){
				if (this.seeMap[tgttid] == false){
					this.refreshTile(tgttid);
				}
			}
		}

		// 计算势力地图，由本方情报提供代价地图，可以看见的敌军位置会被标识为不可到达区域
		public getForcePassIndex(isAvoidEnemy : boolean, isAvoidCrowed : boolean) : number[] {
			let pass_index = this.pass_index.concat();
			if (isAvoidEnemy){
				let troopVisibility = this.getTroopVisibility();
				for (let troopid = 0; troopid < troopVisibility.length; troopid++){
					let troop = Campaign.troop[troopid];
					if (troop.plane != 0) {
						continue;
					}

					if (troop.force != this.id && troopVisibility[troopid]){
						pass_index[ troop.tid ] = -1;
					}
				}
			}
			if (isAvoidCrowed){
				// let crowedMap = troopCrowed.getScriptCrowedMap(troop, tileEngine);
				// for (let tid = 0; tid < tileEngine.tileCount; tid++){
				// 	if (crowedMap[tid] > 3)
				// 		pass_index[tid] = -1;
				// }
			}
			return pass_index;
		}

		public getTroopPassIndex(troopid : number, isAvoidEnemy : boolean, isAvoidCrowed : boolean) : number[]{
			let pass_index = this.getForcePassIndex(isAvoidEnemy, isAvoidCrowed);
			for (let tid = 0; tid < pass_index.length; tid++){
				if (Campaign.troop[troopid].move_ability[Campaign.zone.land_type[tid]] == false){
					pass_index[tid] = -1;
				}
			}
			return pass_index;
		}

		public getTroopVisibility() : boolean[] {
			let troopVisible = [];
			for (let troopid = 0; troopid < Campaign.troop.length; troopid++){
				if (Campaign.troop[troopid].plane < 0) {
					continue;
				}
				troopVisible[troopid] = this.seeMap[Campaign.troop[troopid].tid];
			}
			return troopVisible;
		}
	}
	*/
}